package com.lc.advertising.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppFragment;
import com.lc.advertising.base.BaseDialog;
import com.lc.advertising.dialog.MessageDialog;
import com.lc.advertising.http.request.OrderListApi;
import com.lc.advertising.http.request.OrderQuerenApi;
import com.lc.advertising.http.response.OrderListBean;
import com.lc.advertising.http.response.RegisterBean;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.ui.activity.OrderActivity;
import com.lc.advertising.ui.activity.OrderListDetailsActivity;
import com.lc.advertising.util.SPUtil;
import com.lc.advertising.util.constant.EvenConstant;
import com.lc.advertising.util.eventbus.Event;
import com.lc.advertising.util.eventbus.EventBusUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class OrderListAllFragment extends AppFragment<OrderActivity> {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    List<OrderListBean.QiangdanBean> invirstList = new ArrayList<>();
    private String system_id;
    private int mPage = 1;
    private BaseQuickAdapter productAdapter;
    private int total;
    private String id;

    public static OrderListAllFragment newInstance(String system_id) {
        OrderListAllFragment fragment = new OrderListAllFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, system_id);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            system_id = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_orderlist;
    }

    @Override
    protected void initView() {
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {

                    if (mPage < total) {
                        mPage++;
                        order_list_more();

                        refreshLayout.finishLoadMore();
                    } else {
                        toast("数据全部加载完毕");
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage = 1;
                    order_list();
                    refreshLayout.finishRefresh();
                }, 1000);

            }
        });
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        productAdapter = new BaseQuickAdapter<OrderListBean.QiangdanBean, BaseViewHolder>(R.layout.item_order, invirstList) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, OrderListBean.QiangdanBean databean) {
                if (databean.getCheckinfo() == 2) {

                    holder.setTextColorRes(R.id.tv_staute, R.color.color8f98a7);
                    holder.setBackgroundResource(R.id.tv_staute, R.drawable.jhx_juxing_yuanjiao_gray1);
                    holder.setBackgroundResource(R.id.btn_qiangdan, R.drawable.jhx_juxing_yuanjiao_gray1);
                    holder.setTextColorRes(R.id.btn_qiangdan, R.color.color0055FE);
                    holder.setText(R.id.btn_qiangdan, "查看详情");
                } else {

                    holder.setTextColorRes(R.id.tv_staute, R.color.color00A775);
                    holder.setBackgroundResource(R.id.tv_staute, R.drawable.jhx_juxing_yuanjiao_green);
                    holder.setBackgroundResource(R.id.btn_qiangdan, R.drawable.jhx_juxing_yuanjiao_blue1);
                    holder.setTextColorRes(R.id.btn_qiangdan, R.color.white);
                    holder.setText(R.id.btn_qiangdan, "确认到达目的地");
                }
                holder.setText(R.id.tv_staute, databean.getZhuangtai());
                holder.setText(R.id.tv_start, databean.getBegin_place());
                holder.setText(R.id.tv_end, databean.getEnd_place());
                holder.setText(R.id.time, databean.getCreate_time());
                holder.getView(R.id.btn_qiangdan).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (databean.getCheckinfo() == 2) {
                            startActivity(new Intent(getContext(), OrderListDetailsActivity.class).putExtra("id",databean.getId()+""));
                        }else {
                            new MessageDialog.Builder(getActivity())
                                    // 标题可以不用填写
                                    .setTitle("到达目的地")
                                    // 内容必须要填写
                                    .setMessage("请确认是否到达")
                                    // 确定按钮文本
                                    .setConfirm(getString(R.string.common_confirm))
                                    // 设置 null 表示不显示取消按钮
                                    .setCancel(getString(R.string.common_cancel))
                                    // 设置点击按钮后不关闭对话框
                                    //.setAutoDismiss(false)
                                    .setListener(new MessageDialog.OnListener() {

                                        @Override
                                        public void onConfirm(BaseDialog dialog) {
                                            EventBusUtils.sendEvent(new Event(EvenConstant.ORDER,databean.getId() + ""));


                                        }

                                        @Override
                                        public void onCancel(BaseDialog dialog) {
                                            dialog.dismiss();
                                        }
                                    })
                                    .show();
                        }


                    }
                });
            }
        };
        recyclerview.setAdapter(productAdapter);


    }

    @Override
    protected void initData() {

    }

    private void order_list() {
        EasyHttp.post(this)
                .api(new OrderListApi()
                        .setUid(SPUtil.get(getContext(), IntentKey.UID, "")).setPage(mPage).setType(system_id))
                .request(new HttpCallback<OrderListBean>(this) {

                    @Override
                    public void onSucceed(OrderListBean data) {
//
                        if (data.getCode() == 200) {
                            total = data.getTotal_count();
                            invirstList.clear();
                            invirstList.addAll(data.getQiangdan());
                            productAdapter.setNewData(invirstList);
                            productAdapter.notifyDataSetChanged();
                        }

                    }


                });
    }

    private void order_list_more() {
        EasyHttp.post(this)
                .api(new OrderListApi()
                        .setUid(SPUtil.get(getContext(), IntentKey.UID, "")).setPage(mPage).setType(system_id))
                .request(new HttpCallback<OrderListBean>(this) {

                    @Override
                    public void onSucceed(OrderListBean data) {
//
                        if (data.getCode() == 200) {
                            total = data.getTotal_count();

                            invirstList.addAll(data.getQiangdan());
                            productAdapter.setNewData(invirstList);
                            productAdapter.notifyDataSetChanged();
                        }

                    }


                });
    }

    private void order_queren(String id) {
        EasyHttp.post(this)
                .api(new OrderQuerenApi()
                        .setUid(SPUtil.get(getContext(), IntentKey.UID, "")).setId(id))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMessage());
                        if (data.getCode() == 200) {
                            order_list();
                        }

                    }


                });
    }

    @Override
    public void onResume() {
        super.onResume();
        order_list();
    }
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        super.receiveEvent(event);

        if(event.getCode()== EvenConstant.ORDER) {
            order_queren((String) event.getData());
        }
    }
}
